package com.cskaoyan.market.service.admin;/*
 *文件名: MarketGoodsServiceImpl
 *创建者:wxin
 *创建时间:2024/5/8 22:03
 */

import com.cskaoyan.market.db.domain.MarketOrderGoods;
import com.cskaoyan.market.db.domain.MarketOrderGoodsExample;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.vo.MarketGoodsVo;
import com.cskaoyan.market.vo.MarketGoodsVoRows;
import com.cskaoyan.market.vo.TimeConUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
@Service
public class MarketStatGoodsServiceImpl implements MarketStatGoodsService {
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Override
    public MarketGoodsVo getGoods() {
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());
        Map<String,List<MarketOrderGoods>> goodsGroupByTime=goodsGroupByTime(marketOrderGoods);
        List<MarketGoodsVoRows> rows1= new ArrayList<>();
        for (Map.Entry<String, List<MarketOrderGoods>> stringListEntry : goodsGroupByTime.entrySet()) {
            MarketGoodsVoRows row=getRows(stringListEntry.getKey(),stringListEntry.getValue());
            rows1.add(row);
        }
        MarketGoodsVo marketGoodsVo=new MarketGoodsVo();
        marketGoodsVo.setRows(rows1);
        String[] s1=new String[]{"day","orders","products","amount"};
        marketGoodsVo.setColumns(s1);
        return marketGoodsVo;
    }

    private MarketGoodsVoRows getRows(String key, List<MarketOrderGoods> value) {
        MarketGoodsVoRows r1=new MarketGoodsVoRows();
        r1.setDay(key);
        List<Integer> numList=new ArrayList<>();
        List<BigDecimal> priceList=new ArrayList<>();
        Set<Integer> orders1=new HashSet<>();
        for (MarketOrderGoods marketOrderGoods : value) {
            int  number = marketOrderGoods.getNumber();
            BigDecimal price = marketOrderGoods.getPrice();
            Integer orderId = marketOrderGoods.getOrderId();
            BigDecimal value1 = BigDecimal.valueOf(number);
            BigDecimal multiply = price.multiply(value1);
            numList.add(number);
            priceList.add(multiply);
            orders1.add(orderId);
        }
        int size = orders1.size();
        Integer sum = numList.stream().reduce(Integer::sum).orElse(0);
        BigDecimal sum1=priceList.stream().reduce(BigDecimal.ZERO,BigDecimal::add);
        r1.setOrders(size);
        r1.setProducts(sum);
        r1.setAmount(sum1);
        return r1;
    }

    private Map<String, List<MarketOrderGoods>> goodsGroupByTime(List<MarketOrderGoods> marketOrderGoods) {
        Map<String,List<MarketOrderGoods>> listMap=new TreeMap<>();
        for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
            LocalDateTime updateTime = marketOrderGood.getUpdateTime();
            if(updateTime!=null){
                String timeCon1 = TimeConUtil.TimeCon(updateTime);
                List<MarketOrderGoods> list=listMap.get(timeCon1);
                if(list==null){
                    list=new ArrayList<>();
                }
                list.add(marketOrderGood);
                listMap.put(timeCon1,list);
            }
        }
        return listMap;
    }
}